import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;

import javax.imageio.ImageIO;


public class MyServer {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		ServerSocket serverSocket = new ServerSocket(42083);
		
		Socket s = serverSocket.accept();
		System.out.println("Connection from " + s.getInetAddress().getHostAddress());
		BufferedReader cbr = new BufferedReader(new InputStreamReader(s.getInputStream()));
		OutputStream out = s.getOutputStream();
		String inputLine = cbr.readLine();
		if(inputLine == null){
			System.out.println("read null from input");
			System.exit(-1);
		}
		System.out.println("Received: " + inputLine);
		
		byte[] bytes = new byte[8];
		ByteBuffer.wrap(bytes).putDouble(1332.25);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		out.write(bytes);
		
		BufferedOutputStream bos = new BufferedOutputStream(out);
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream("IMG_0687.JPG"));
		byte[] data = new byte[1024];
		int r; 
		int bytesRead = 0;
		while( (r = bis.read(data)) != -1){
			
			bos.write(data, 0, r);
			bos.flush();
			bytesRead+=r;
			if(bytesRead == 250880){
				System.out.println(r);
			}
		}
		System.out.println(bytesRead);
		bis.close();
		bos.close();
		
		
		
		s.close();
		
	}

}
